package com.qf.controller;

import com.qf.service.*;
import com.qf.util.ResponseUtil;
import com.qf.vo.CategorySellAmts;
import com.qf.vo.CategorySellVo;
import com.qf.vo.DayStatis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.ws.Service;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 首页
 * @author bss
 * @version V1.0
 * @Project maven
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/10/13 20:41
 */
@RestController
@RequestMapping("/admin/dashboard")
public class AdminDashboardController {


    @Autowired
    DtsUserService userService;

    @Autowired
    DtsGoodsService goodsService;

    @Autowired
    DtsGoodsproductService productService;

    @Autowired
    DtsOrderService orderService;

    @Autowired
    StatService statService;

//获取用户，商品，货品，货品数量
    @RequestMapping("")
    public Object dashboard(){

        long userTotal = userService.getTotal();
        long goodsTotal = goodsService.getTotal();
        long productTotal = productService.getTotal();
        long orderTotal = orderService.getTotal();


        Map<String ,Object> data = new HashMap<>();
        data.put("goodsTotal", goodsTotal);
        data.put("userTotal", userTotal);
        data.put("productTotal", productTotal);
        data.put("orderTotal", orderTotal);

        return ResponseUtil.ok(data);
    }

    @RequestMapping("/chart")
    public Object chart(){
//        饼状图
//        获取商品分类 和销售额  统计数据
        List<CategorySellAmts> categorySellData = statService.findCategorySell();
//        分类名称
        String[] categoryName = new String[categorySellData.size()];
        for (int i = 0; i < categorySellData.size(); i++) {
            categoryName[i] = categorySellData.get(i).getName();
        }
        CategorySellVo categorySellVo = new CategorySellVo();
        categorySellVo.setCategoryNames(categoryName);
        categorySellVo.setCategorySellData(categorySellData);

//        树状图
//        获取 日期和对应的订单总额 订饭数量
//        查询 最近一个月内（大于9月13号） 对应的日期，订单总额，订单笔数
        List<DayStatis> orderDayStatis = statService.findOrderDayStatis();
//        提取日期
        List<String> dayStr = fetchDayStr(orderDayStatis);
//        提取 订单的总额
        List<BigDecimal> orderAmtData = fetchAmtData(orderDayStatis, dayStr);
//        提取订单的数量
        List<Integer> orderCntData = fetchCntData(orderDayStatis, dayStr);
//        封装 订单数据
        Map<String ,Object> orderAmts = new HashMap<>();
        orderAmts.put("dayData", dayStr);
        orderAmts.put("orderAmtData",orderAmtData );
        orderAmts.put("orderCntData",orderCntData );

//        Map<String ,Object> categorySell = new HashMap<>();
//        categorySell.put("categoryName", categorySellVo);
//        categorySell.put("categorySellData", categorySellData);

//userOrderCnt : 日期对应的用户数和订单数
//        查询用户的数量
        List<DayStatis> userDayStatis = statService.findUserDayStatis();
        List<String> useData = fetchDayStr(userDayStatis);
//        两个日期合并
        useData = unionDayData(dayStr,useData);
//        提取User的数量
        List<Integer> userCnt = fetchCntData(userDayStatis, useData);
//        提取订单的数量
        List<Integer> orderCnt = fetchCntData(orderDayStatis, useData);

        Map<String, Object> userOrderCnt = new HashMap<>();
        userOrderCnt.put("dayData", useData);
        userOrderCnt.put("userCnt", userCnt);
        userOrderCnt.put("orderCnt", orderCnt);

        Map<String ,Object> data = new HashMap<>();
        data.put("categorySell", categorySellVo);
        data.put("orderAmts", orderAmts);
        data.put("userOrderCnt", userOrderCnt);


        return ResponseUtil.ok(data);
    }

    private List<String> unionDayData(List<String> dayStr, List<String> useData) {
        Set<String> set = new HashSet<>();
        //把两个list集合的元素都添加到set集合中， 自动去重
        set.addAll(dayStr);
        set.addAll(useData);

        List<String> list = new ArrayList<>();
        list.addAll(set);

        Collections.sort(list);
        return list;
    }

    private List<BigDecimal> fetchAmtData(List<DayStatis> dayStatis, List<String> dayStr) {
        List<BigDecimal> list = new ArrayList<>();
        //循环每一个日期
        for (String day : dayStr) {
            for (DayStatis dayStati : dayStatis) {
                //如果日期匹配，则取出 订单总额
                if (day.equalsIgnoreCase(dayStati.getDayStr())){
                    list.add(dayStati.getAmts());
                }
            }
        }
        return list;
    }

    private List<Integer> fetchCntData(List<DayStatis> dayStatis, List<String> dayStr) {
        List<Integer> list = new ArrayList<>();
        //循环每一个日期
        for (String day : dayStr) {
            for (DayStatis dayStati : dayStatis) {
                //如果日期匹配，则取出 订单数量
                if (day.equalsIgnoreCase(dayStati.getDayStr())){
                    list.add(dayStati.getCnts());
                }
            }
        }
        return list;
    }

    private List<String> fetchDayStr(List<DayStatis> dayStatis) {
        List<String> list = dayStatis.stream().map(dayStatis1 -> {
            return dayStatis1.getDayStr();
        }).collect(Collectors.toList());

        Collections.sort(list);
        return list;

    }

}
